package tech.xiaozai.exam.mapstruct;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import tech.xiaozai.enums.QuestionType;
import tech.xiaozai.exam.bo.ExamQuestionBO;
import tech.xiaozai.exam.po.ExamQuestion;

import java.util.List;


/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-09-21 09:47
 */

@Mapper
public interface ExamQuestionMapStruct {

    ExamQuestionMapStruct me = Mappers.getMapper(ExamQuestionMapStruct.class);


    /**
     *   将试题BO转PO
     * @param bo
     * @return
     */
    default ExamQuestion toPo(ExamQuestionBO bo){
        ExamQuestion po = new ExamQuestion();
        po.setId(bo.getId());
        po.setSubjectId(bo.getSubjectId());
        po.setAnswer(JSONUtil.toJsonStr(bo.getAnswer()));
        po.setQuestion(JSONUtil.toJsonStr(bo.getQuestion()));
        po.setCreatedBy(bo.getCreatedBy());
        po.setCreateTime(bo.getCreateTime());
        po.setUpdateTime(null);
        po.setQuestionType(bo.getQuestionType().name());
        return po;
    }

    default ExamQuestionBO toBo(ExamQuestion po){
        ExamQuestionBO bo = new ExamQuestionBO();
        bo.setAnswer( JSONUtil.parseArray(po.getAnswer()) );
        bo.setQuestion(JSONUtil.parseObj(po.getQuestion()) );
        bo.setCreatedBy(po.getCreatedBy());
        bo.setCreateTime(po.getCreateTime());
        bo.setUpdateTime(po.getUpdateTime());
        bo.setId(po.getId());
        bo.setSubjectId(po.getSubjectId());
        bo.setQuestionType(QuestionType.valueOf(po.getQuestionType()));
        return bo;
    }

    List<ExamQuestionBO> toBos(List<ExamQuestion> pos);
}
